//
//  controlView.m
//  NewMusic
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "controlView.h"
#import "PlayerViewController.h"
#import <STKAudioPlayer.h>
#import <UIImageView+WebCache.h>
@interface controlView ()
@property (nonatomic, retain) PlayerViewController *playerView;
@property (nonatomic, retain) UIImageView *imageState;
@property (nonatomic, retain) UIImageView *imageViewToPlayer;
@property (nonatomic, retain) UIImageView *imageViewNext;
@property (nonatomic, retain) UIImageView *imageViewLast;
@property (nonatomic, retain) UILabel *labelTitle;
@property (nonatomic, retain) UIImageView *imageHead;
@end

@implementation controlView
- (void)dealloc{
    [_playerView release];
    [_imageState release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"statePlayer" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLabelAndImageView" object:nil];
    [_imageViewLast release];
    [_imageViewNext release];
    [_imageHead release];
    [_labelTitle release];
    [_imageViewToPlayer release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView
{
    UIView *RightView = [[UIView alloc] initWithFrame:CGRectMake(225, 0, 150, 60)];
    RightView.backgroundColor = [UIColor clearColor];
    [self addSubview:RightView];
    [RightView release];
    
    self.playerView = [PlayerViewController sharePlayerViewController];
    self.imageState = [[UIImageView alloc] initWithFrame:CGRectMake(30, 15, 20, 20)];
    self.imageState.userInteractionEnabled = YES;
    if (STKAudioPlayerStatePlaying == self.playerView.player.state || STKAudioPlayerStateError == self.playerView.player.state)
    {
        self.imageState.image = [UIImage imageNamed:@"BF-player"];
    } else {
        self.imageState.image = [UIImage imageNamed:@"BF-stop"];
    }
    [RightView addSubview:self.imageState];
    [_imageState release];
    
    UITapGestureRecognizer *tapState = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChangeState)];
    [self.imageState addGestureRecognizer:tapState];
    [tapState release];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ImageViewStatePlayer) name:@"statePlayer" object:nil];
    
    self.imageViewToPlayer = [[UIImageView alloc] initWithFrame:CGRectMake(90, 15, 20, 20)];
    self.imageViewToPlayer.image = [UIImage imageNamed:@"BF-toplayerView"];
    [RightView addSubview:self.imageViewToPlayer];
    self.imageViewToPlayer.userInteractionEnabled = YES;
    [_imageViewToPlayer release];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0.0]];
    [animation setDuration:2.0];
    animation.repeatCount = HUGE_VALF;
    [_imageViewToPlayer.layer addAnimation:animation forKey:nil];
    UITapGestureRecognizer *tapPlayer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPlayer)];
    [self.imageViewToPlayer addGestureRecognizer:tapPlayer];
    [tapPlayer release];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 225, 60)];
    [self addSubview:leftView];
    [leftView release];
    
    self.imageViewLast = [[UIImageView alloc] initWithFrame:CGRectMake(30, 15, 20, 20)];
    self.imageViewLast.userInteractionEnabled = YES;
    self.imageViewLast.image = [UIImage imageNamed:@"BF-last"];
    [leftView addSubview:self.imageViewLast];
    [_imageViewLast release];
    self.imageViewNext = [[UIImageView alloc] initWithFrame:CGRectMake(180, 15, 20, 20)];
    self.imageViewNext.userInteractionEnabled = YES;
    self.imageViewNext.image = [UIImage imageNamed:@"BF-next"];
    [leftView addSubview:self.imageViewNext];
    [_imageViewNext release];
    
    self.imageHead = [[UIImageView alloc] initWithFrame:CGRectMake(60, 5, 40, 40)];
    self.imageHead.layer.masksToBounds = YES;
    self.imageHead.highlightedImage = 
    self.imageHead.image = [UIImage imageNamed:@"sd.png"];
    self.imageHead.layer.cornerRadius = 20;
    [leftView addSubview:self.imageHead];
    [_imageHead release];
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(110, 2, 50, 50)];
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.text = @"用心聆听音乐";
    self.labelTitle.font = [UIFont systemFontOfSize:14];
    self.labelTitle.numberOfLines = 2;
    [leftView addSubview:self.labelTitle];
    [_labelTitle release];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelAndImageView) name:@"changeLabelAndImageView" object:nil];
    
    UITapGestureRecognizer *tapLast = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLast)];
    [self.imageViewLast addGestureRecognizer:tapLast];
    [tapLast release];
    
    UITapGestureRecognizer *tapNext = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNext)];
    [self.imageViewNext addGestureRecognizer:tapNext];
    [tapNext release];
    
}

- (void)tapNext
{
    [self.playerView changeNumWith:NO];
}
- (void)tapLast
{
    [self.playerView changeNumWith:YES];
}
- (void)changeLabelAndImageView
{
    self.labelTitle.text = self.playerView.labelText;
    if (self.playerView.picUrl.length != 0)
    {
        [self.imageHead sd_setImageWithURLStr:self.playerView.picUrl];
    } else {
        self.imageHead.image = [UIImage imageNamed:@"sd.png"];
    }
    
}
- (void)animation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0.0]];
    [animation setDuration:2.0];
    animation.repeatCount = HUGE_VALF;
    [self.imageViewToPlayer.layer addAnimation:animation forKey:nil];
}
- (void)tapPlayer
{
    [self.delegate toPlayerView];
}
- (void)tapChangeState
{
    if (STKAudioPlayerStatePlaying == self.playerView.player.state)
    {
        self.imageState.image = [UIImage imageNamed:@"BF-player"];
        [self.playerView stopMusic];
    } else {
        self.imageState.image =  [UIImage imageNamed:@"BF-stop"];
        [self.playerView StartMusic];
    }
}
- (void)ImageViewStatePlayer
{
    if (STKAudioPlayerStatePlaying == self.playerView.player.state || STKAudioPlayerStateBuffering == self.playerView.player.state)
    {
        self.imageState.image = [UIImage imageNamed:@"BF-stop"];
    } else {
        self.imageState.image =  [UIImage imageNamed:@"BF-player"];
    }
}

@end
